package com.hq.modeapp.ui;



import com.hq.modeapp.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.ViewFlipper;

/**
 * **********************************************************
 *  @内容摘要	：引导页
 *
 *  @作者	：黄秋
 *  @创建时间	：2013-4-11 下午3:16:11 
 *  @历史记录	:
 *  @日期	: 2013-4-11 下午3:16:11 	修改人：黄秋
 *  @描述	:
 ***********************************************************
 */
public class GuideActivity extends Activity implements OnGestureListener {

	private ViewFlipper viewFlipper;
	private GestureDetector detector; // 手势检测
	private int currindex;// 当前页
	private int counts;// 总的页数
	private Button btn_guidepage_start;// 开始体验
	// 引导图片资源
	private int[] guide_pics = { R.drawable.guide_pic1, R.drawable.guide_pic2,
			R.drawable.guide_pic3,R.drawable.guide_pic3 };
	private Animation leftInAnimation;
	private Animation leftOutAnimation;
	private Animation rightInAnimation;
	private Animation rightOutAnimation;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.guide_activity);
		viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);
		detector = new GestureDetector(this);
		// 往viewFlipper添加View
		for (int guide_pic : guide_pics) {
			viewFlipper.addView(getImageView(guide_pic));
		}
		// 动画效果
		leftInAnimation = AnimationUtils.loadAnimation(this, R.anim.left_in);
		leftOutAnimation = AnimationUtils.loadAnimation(this, R.anim.left_out);
		rightInAnimation = AnimationUtils.loadAnimation(this, R.anim.right_in);
		rightOutAnimation = AnimationUtils
				.loadAnimation(this, R.anim.right_out);
		btn_guidepage_start = (Button) findViewById(R.id.btn_guidepage_start);
		btn_guidepage_start.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				System.out.println("======开始体验======");
				finish();
				 Intent intent = new Intent(GuideActivity.this,MainActivity.class);
				 startActivity(intent);
				 

			}
		});
		currindex = 0;
		counts = viewFlipper.getChildCount();
	}

	private ImageView getImageView(int id) {
		ImageView imageView = new ImageView(this);
		imageView.setScaleType(ScaleType.FIT_XY);
		imageView.setImageResource(id);
		return imageView;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return this.detector.onTouchEvent(event); // touch事件交给手势处理。
	}

	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub

		return false;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		if (e1.getX() - e2.getX() > 120) {
			if (currindex < counts - 1) {
				viewFlipper.setInAnimation(leftInAnimation);
				viewFlipper.setOutAnimation(leftOutAnimation);
				viewFlipper.showNext();// 向左滑动
				currindex++;
			}
			if (currindex == (counts - 1)) {
				btn_guidepage_start.setVisibility(View.VISIBLE);
			} else {
				btn_guidepage_start.setVisibility(View.GONE);
			}
			return true;
		} else if (e1.getX() - e2.getX() < -120) {
			if (currindex > 0) {
				viewFlipper.setInAnimation(rightInAnimation);
				viewFlipper.setOutAnimation(rightOutAnimation);
				viewFlipper.showPrevious();// 向右滑动
				currindex--;
			}
			if (currindex == (counts - 1)) {
				btn_guidepage_start.setVisibility(View.VISIBLE);
			} else {
				btn_guidepage_start.setVisibility(View.GONE);
			}
			return true;
		}

		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {

	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {

	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {

		return false;
	}

}
